package tempo.timing;

import tempo.timing.SimpleGestureFilter.SimpleGestureListener;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TabHost;

public class MainWindow extends TabActivity implements SimpleGestureListener
{
	// private static String TAG = "TempoTiming";
	private SimpleGestureFilter gestureDetector;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		gestureDetector = new SimpleGestureFilter(this, this);

		TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; // Resusable TabSpec for each tab
		Intent intent; // Reusable Intent for each tab
		Drawable icon; // Reusable Icon for each tab

		// Create an Intent to launch an Activity for the tab (to be reused)
		icon = getResources().getDrawable(R.drawable.icon_adduser);
		intent = new Intent().setClass(this, AddRider.class);
		spec = tabHost.newTabSpec("addRider").setIndicator("Add rider", icon).setContent(intent); // res.getDrawable(R.drawable.ic_tab_artists)
		tabHost.addTab(spec);

		// Create the start tab
		icon = getResources().getDrawable(R.drawable.icon_greenflag);
		intent = new Intent().setClass(this, StartActivity.class);
		spec = tabHost.newTabSpec("start").setIndicator("Start", icon).setContent(intent); // res.getDrawable(R.drawable.ic_tab_artists)
		tabHost.addTab(spec);

		// Create the finish tab
		icon = getResources().getDrawable(R.drawable.icon_finishflag);
		intent = new Intent().setClass(this, FinishActivity.class);
		spec = tabHost.newTabSpec("finish").setIndicator("Finish", icon).setContent(intent); // res.getDrawable(R.drawable.ic_tab_albums)
		tabHost.addTab(spec);

		// Create the results tab
		icon = getResources().getDrawable(R.drawable.icon_podium);
		intent = new Intent().setClass(this, ResultActivity.class);
		spec = tabHost.newTabSpec("result").setIndicator("Result", icon).setContent(intent); // res.getDrawable(R.drawable.ic_tab_songs)
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);
		Race.getCurrent().setContext(this);
		LoadJson();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.mnuRaceInfo:
			// show raceInfo
			Intent intent = new Intent().setClass(this, RaceInfo.class);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void LoadJson()
	{
		Race.getCurrent().LoadJson();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent me)
	{
		this.gestureDetector.onTouchEvent(me);
		return super.dispatchTouchEvent(me);
	}

	public void onDoubleTap()
	{
	}

	public void onSwipe(int direction)
	{
		switch (direction)
		{
		case SimpleGestureFilter.SWIPE_RIGHT:
			HandleRightSwipe();
			break;
		case SimpleGestureFilter.SWIPE_LEFT:
			HandleLeftSwipe();
			break;
		}
	}

	private void HandleRightSwipe()
	{
		TabHost tabHost = getTabHost();
		int currentTab = tabHost.getCurrentTab();
		if (currentTab > 0)
			tabHost.setCurrentTab(--currentTab);
	}

	private void HandleLeftSwipe()
	{
		TabHost tabHost = getTabHost();
		int numberOfTabs = tabHost.getTabWidget().getChildCount();// getChildCount();
		int currentTab = tabHost.getCurrentTab();
		if (currentTab < (numberOfTabs - 1))
			tabHost.setCurrentTab(++currentTab);
	}

}